/**
 * 
 */
package TestScenario;

import ptvv.*;
import ptvv.system.*;

/**
 * @author dkz
 * 
 */
public class Delay extends DefPortAutomaton {
	// Private Variables

	public Delay(ThreadAndClass dest) {
		this.setName("NoNameDelay");
		// this.setOutputPort(dest, 0, 0);

	}

	public Delay(String name) {
		this.setName(name);

	}

	public void run() {
		while (internalState != InternalStates.ReallyDead)
			runStates();

		// logger.log("SHUTDOWN");
	}

	public void starting() throws InterruptedException {

		String item = eventQueue.take();
		// System.out.println(item);

	}

	public void ready() throws InterruptedException {
		// String temp = inputPorts.get(0).queue.poll();
		// if (temp != null)
		// message = temp;
		// else
		message = "1"; // default delay
	}

	public void running() throws InterruptedException {
		// Thread.sleep(Long.parseLong(message));
		Thread.sleep(Long.parseLong(message) * 1000);
		// System.out.println("sleeping..");
		// System.out.println("awake");
	}

	public void suspended() throws InterruptedException {

	}

	public void end() {
		// System.out.println("end");
		logger.log("" + this.getName() + " : " + message);
		// logger.log(this.getName() + " num: " + this.outputPorts.size());
		logger.packet(new TracePacket(this.getName(), message));
		for (OutputPort op : this.outputPorts) {
			//System.out.println("sending interrupt" + op.destThread.getState());
			op.interruptThread();

		}
		// repeatBool = true;
	}

	public void dead() {
		// this.internalState = InternalStates.Start;

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {

	}

}
